package com.chy.edu.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chy.commonutils.JwtUtils;
import com.chy.commonutils.Result;
import com.chy.commonutils.ordervo.CourseWebVoOrder;
import com.chy.edu.client.OrdersClient;
import com.chy.edu.pojo.EduCourse;
import com.chy.edu.pojo.EduTeacher;
import com.chy.edu.pojo.chapter.ChapterVo;
import com.chy.edu.pojo.frontvo.CourseFrontVo;
import com.chy.edu.pojo.frontvo.CourseWebVo;
import com.chy.edu.service.EduChapterService;
import com.chy.edu.service.EduCourseService;
import com.chy.edu.service.EduTeacherService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrdersClient ordersClient;

    @ApiOperation(value = "条件带分页查询课程")
    @PostMapping("getFrontCourseList/{current}/{size}")
    public Result getFrontCourseList(@ApiParam(name = "current", value = "当前页码", required = true)
                                         @PathVariable Long current,
                                     @ApiParam(name = "size", value = "每页记录数", required = true)
                                         @PathVariable Long size,
                                     @ApiParam(name = "courseFrontVo", value = "查询对象")
                                         @RequestBody(required = false) CourseFrontVo courseFrontVo)
    {
        Page<EduCourse> pageCourse = new Page<>(current,size);
        Map<String, Object> map = courseService.getCourseFrontList(pageCourse,courseFrontVo);
        // 返回分页所有数据
        return Result.ok().data(map);
    }

    @ApiOperation(value = "课程详情")
    @GetMapping("getFrontCourseInfo/{courseId}")
    public Result getFrontCourseInfo(@ApiParam(name = "courseId", value = "课程ID", required = true)
                                         @PathVariable String courseId, HttpServletRequest request)
    {
        // 根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);

        // 根据课程id，查询章节和小节
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);

        // 根据课程id和用户id查询当前课程是否已经支付过了
        boolean buyCourse = ordersClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));

        return Result.ok()
                .data("courseWebVo",courseWebVo)
                .data("chapterVideoList",chapterVideoList)
                .data("isBuy",buyCourse);
    }

    @ApiOperation(value = "根据课程id查询课程信息")
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@ApiParam(name = "id", value = "课程ID", required = true)
                                                   @PathVariable String id)
    {
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);
        return courseWebVoOrder;
    }
}
